/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-02-12 10:53:14
 * @filename : static/site/nff/controller/emailNav.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {
    var ComposeView, nav='emailNav', 
        mId = 'emailNav', reRender = true, menuName = 'emailNav',emailType,emailName,
        tpl = require('{view}emailNav.tpl'),
        tplL = require('{view}emailNavLi.tpl'),
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tplN = require('{view}userNav.tpl');

    ComposeView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),
        templateL: _.template(tplL),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
            window.scroll(0,0);
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template());
                this.bindEvt();            
                //this.getFolderList();
                this.getFolderListState(); 
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
            
            if (obj.val) {
                 var arr = obj.val.split('##');
                 emailType = arr[0];
                 emailName = arr[1];
            } else {
                 emailType = '.Inbox';
                 emailName = 'Emails';
            }
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);    
            dom.find('.back').on('tap',function(e){
                e.stopPropagation();
                window.location.href = '#emailList/whole/custom'+emailType+'##'+emailName;
            }); 

            dom.find('.emailTool').on('tap',function(e){
                var type = $(e.target).closest('li').attr('type'),
                    name = $(e.target).closest('li').attr('name');
                window.location.href = '#emailList/whole/custom'+type+'##'+name;
            });
        },
            
        getFolderListState : function() {
            var tthis = this,
                dom =  $('#'+mId).find('.myFolders');

            var obj = {
                url : seajs.data.vars.apiAccessUrl+'wm_get_folders_status',
                type : 'POST',
                data : '',
                success : function(data) {
                    if (data.ret) {
                        //tthis.showNum(tthis.getArrName('.Inbox',data.status));
                        dom.append(tthis.templateL({arr:data.status}));
                    }
                },
                error : function(xhr, errorType, error) {},
                complete : function(xhr, status) {
                    share.ajaxEndB(dom);
                }
            }
            share.ajaxStartB(dom);
            $.ajax(share.ajaxControl(obj));
        },

        showNum : function(count) {
              if (count > 0 ) {
                  $('#'+mId).find('.email_Inbox').find('.email_num').html(count).css('display','inline'); 
              } else  {
                  $('#'+mId).find('.email_Inbox').find('.email_num').html('').css('display','none');
              } 
        },

        getArrName : function(name,arr) {
            var num;
            for (var i in arr) {
                if (arr[i].folder == name) {
                    num = arr[i].msgs_all;
                }
            }
            return parseInt(num);
        },

    });
    module.exports = ComposeView;
});

